import {useRoutes} from "react-router-dom";
import { Suspense, lazy } from 'react';
// import { Spin } from 'antd';
import Loading from "../components/Loading";
//路由配置
const routes = [
  {
    path: '/',
    auth:true,
    component:lazy(() => import('../view/Login'))
  },
  {
    path:'/home',
    auth:true,
    component:lazy(() => import('../view/Home')),
    children:[
      {
        path:'/home/data',
        auth:true,
        component:lazy(() => import('../view/Home/Data'))
      },
      {
        path:'/home/order',
        auth:true,
        component:lazy(() => import('../view/Home/Order'))
      },
      {
        path:'/home/variety',
        auth:true,
        component:lazy(() => import('../view/Home/Variety')),
      },
      {
        path:'/home/classification',
        auth:true,
        component:lazy(() => import('../view/Home/Classification')),
      },
      {
        path:'/home/table_num',
        auth:true,
        component:lazy(() => import('../view/Home/TableNum')),
      },
      {
        path:'/home/norm',
        auth:true,
        component:lazy(() => import('../view/Home/Norm')),
      },
      {
        path:'/home/shop',
        auth:true,
        component:lazy(() => import('../view/Home/Shop')),
      }
    ]
  },
  {
    path:'/*',
    auth:true,
    component:lazy(() => import('../view/Error')),
    code:'all',
  }
]
//根据路径获取路由
const checkAuth = (routers, path)=>{
  for (const data of routers) {
    if (data.path===path) return data
    if (data.children) {
      const res = checkAuth(data.children, path)
      if (res) return res
    }
  }
  return null
}
// 路由处理方式
const  generateRouter = (routers) => {
  return routers.map((item) => {
    if (item.children) {
      item.children = generateRouter(item.children)
    }

    item.element = <Suspense fallback={
      <div className="example">
        <Loading flag={true}></Loading>
      </div>
    }>
      {/* 把懒加载的异步路由变成组件装载进去 */}
      <item.component/>
    </Suspense>
    return item
  })
}
const Router = () => useRoutes(generateRouter(routes))
const checkRouterAuth = (path)=>{
  let auth = checkAuth(routes,path)
  return auth
}
export{ Router,checkRouterAuth}